package com.stcc.portal.interceptor;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述:拦截器
 * 项目名:wisdom_match-parent
 * 包名:com.tydic.portal.interceptor
 * 作者:stcc
 * 创建时间:2021/4/13 22:28
 * 版本:1.0
 */
@Component
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    @Value("${isOpenInterceptor}")
    private Boolean isOpenInterceptor;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //校验是否开启拦截器
       if(true){
            //ip:/rest/pc/portal/matchImChat/qryMatchImChat
     /*        String ip = IpAddressUtil.getIpAddress(request);
            String ip_uri = ip +":"+request.getRequestURI();
            LOGGER.info("-------ip------"+ip);
            Long count = cacheService.getIncr(ip_uri);
            if (null == count || -1 ==count) {
                cacheService.incrExpireTimeBy(ip_uri,1,maxTime);
                return true;
            }else if (count<maxCount) {
                cacheService.incr(ip_uri);
                return true;
            }else if (count>=maxCount){
                // response 返回 json 请求过于频繁请稍后再试
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("result", "MyInterceptor");
                jsonObject.put("MESSAGE", "恶意访问，已拦截!");
                String jsonString = JSON.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
                out.write(jsonString);
                out.close();
                LOGGER.error("该用户在请求链接时存在恶意访问，已拦截："+ip_uri);
                return false;
            }*/
           return true;
        }
        return true;
    }

}
